using System;
using System.Globalization;
using System.IO;
using System.Numerics;
using ns18;
using ns20;

namespace Newtonsoft.Json
{
	public class JsonTextWriter : JsonWriter
	{
		private readonly TextWriter textWriter_0;

		private Class143 class143_0;

		private char char_0;

		private int int_0;

		private char char_1;

		private bool bool_1;

		private bool[] bool_2;

		private char[] char_2;

		private Class143 Class143_0
		{
			get
			{
				if (class143_0 == null)
				{
					class143_0 = new Class143(textWriter_0);
				}
				return class143_0;
			}
		}

		public int Indentation
		{
			get
			{
				return int_0;
			}
			set
			{
				if (value < 0)
				{
					throw new ArgumentException("Indentation value must be greater than 0.");
				}
				int_0 = value;
			}
		}

		public char QuoteChar
		{
			get
			{
				return char_1;
			}
			set
			{
				if (value != '"' && value != '\'')
				{
					throw new ArgumentException("Invalid JavaScript string quote character. Valid quote characters are ' and \".");
				}
				char_1 = value;
				method_20();
			}
		}

		public char IndentChar
		{
			get
			{
				return char_0;
			}
			set
			{
				char_0 = value;
			}
		}

		public bool QuoteName
		{
			get
			{
				return bool_1;
			}
			set
			{
				bool_1 = value;
			}
		}

		public JsonTextWriter(TextWriter textWriter)
		{
			if (textWriter == null)
			{
				throw new ArgumentNullException("textWriter");
			}
			textWriter_0 = textWriter;
			char_1 = '"';
			bool_1 = true;
			char_0 = ' ';
			int_0 = 2;
			method_20();
		}

		public override void Flush()
		{
			textWriter_0.Flush();
		}

		public override void Close()
		{
			base.Close();
			if (base.CloseOutput && textWriter_0 != null)
			{
				textWriter_0.Close();
			}
		}

		public override void WriteStartObject()
		{
			method_16(JsonToken.StartObject, Enum12.Object);
			textWriter_0.Write("{");
		}

		public override void WriteStartArray()
		{
			method_16(JsonToken.StartArray, Enum12.Array);
			textWriter_0.Write("[");
		}

		public override void WriteStartConstructor(string name)
		{
			method_16(JsonToken.StartConstructor, Enum12.Constructor);
			textWriter_0.Write("new ");
			textWriter_0.Write(name);
			textWriter_0.Write("(");
		}

		protected override void WriteEnd(JsonToken token)
		{
			switch (token)
			{
			default:
				throw JsonWriterException.smethod_1(this, "Invalid JsonToken: " + token, null);
			case JsonToken.EndObject:
				textWriter_0.Write("}");
				break;
			case JsonToken.EndArray:
				textWriter_0.Write("]");
				break;
			case JsonToken.EndConstructor:
				textWriter_0.Write(")");
				break;
			}
		}

		public override void WritePropertyName(string name)
		{
			method_14(name);
			method_22(name);
			textWriter_0.Write(':');
		}

		public override void WritePropertyName(string name, bool escape)
		{
			method_14(name);
			if (escape)
			{
				method_22(name);
			}
			else
			{
				if (bool_1)
				{
					textWriter_0.Write(char_1);
				}
				textWriter_0.Write(name);
				if (bool_1)
				{
					textWriter_0.Write(char_1);
				}
			}
			textWriter_0.Write(':');
		}

		internal override void Newtonsoft_002EJson_002EJsonWriter_002E_202B_202B_200D_202C_206D_200B_200C_202E_206F_206C_202B_206C_200D_200F_202D_202C_202D_206A_206D_202A_202E_200D_206C_200B_202B_200B_206E_200C_206A_200B_200F_202E_202C_206C_202B_202B_202D_202D_206D_200F_202E()
		{
			method_20();
		}

		private void method_20()
		{
			if (base.StringEscapeHandling == StringEscapeHandling.EscapeHtml)
			{
				bool_2 = Class190.bool_2;
			}
			else if (char_1 == '"')
			{
				bool_2 = Class190.bool_1;
			}
			else
			{
				bool_2 = Class190.bool_0;
			}
		}

		protected override void WriteIndent()
		{
			textWriter_0.Write(Environment.NewLine);
			int num = base.Top * int_0;
			while (num > 0)
			{
				int num2 = Math.Min(num, 10);
				textWriter_0.Write(new string(char_0, num2));
				num -= num2;
			}
		}

		protected override void WriteValueDelimiter()
		{
			textWriter_0.Write(',');
		}

		protected override void WriteIndentSpace()
		{
			textWriter_0.Write(' ');
		}

		private void method_21(string string_1, JsonToken jsonToken_0)
		{
			textWriter_0.Write(string_1);
		}

		public override void WriteValue(object value)
		{
			if (value is BigInteger)
			{
				method_17(JsonToken.Integer);
				method_21(((BigInteger)value).ToString(CultureInfo.InvariantCulture), JsonToken.String);
			}
			else
			{
				base.WriteValue(value);
			}
		}

		public override void WriteNull()
		{
			method_17(JsonToken.Null);
			method_21(JsonConvert.Null, JsonToken.Null);
		}

		public override void WriteUndefined()
		{
			method_17(JsonToken.Undefined);
			method_21(JsonConvert.Undefined, JsonToken.Undefined);
		}

		public override void WriteRaw(string json)
		{
			method_15();
			textWriter_0.Write(json);
		}

		public override void WriteValue(string value)
		{
			method_17(JsonToken.String);
			if (value == null)
			{
				method_21(JsonConvert.Null, JsonToken.Null);
			}
			else
			{
				method_22(value);
			}
		}

		private void method_22(string string_1)
		{
			method_23();
			Class190.smethod_0(textWriter_0, string_1, char_1, bool_3: true, bool_2, base.StringEscapeHandling, ref char_2);
		}

		public override void WriteValue(int value)
		{
			method_17(JsonToken.Integer);
			method_24(value);
		}

		[CLSCompliant(false)]
		public override void WriteValue(uint value)
		{
			method_17(JsonToken.Integer);
			method_24(value);
		}

		public override void WriteValue(long value)
		{
			method_17(JsonToken.Integer);
			method_24(value);
		}

		[CLSCompliant(false)]
		public override void WriteValue(ulong value)
		{
			method_17(JsonToken.Integer);
			method_25(value);
		}

		public override void WriteValue(float value)
		{
			method_17(JsonToken.Float);
			method_21(JsonConvert.smethod_1(value, base.FloatFormatHandling, QuoteChar, bool_0: false), JsonToken.Float);
		}

		public override void WriteValue(float? value)
		{
			if (!value.HasValue)
			{
				WriteNull();
				return;
			}
			method_17(JsonToken.Float);
			method_21(JsonConvert.smethod_1(value.Value, base.FloatFormatHandling, QuoteChar, bool_0: true), JsonToken.Float);
		}

		public override void WriteValue(double value)
		{
			method_17(JsonToken.Float);
			method_21(JsonConvert.smethod_3(value, base.FloatFormatHandling, QuoteChar, bool_0: false), JsonToken.Float);
		}

		public override void WriteValue(double? value)
		{
			if (!value.HasValue)
			{
				WriteNull();
				return;
			}
			method_17(JsonToken.Float);
			method_21(JsonConvert.smethod_3(value.Value, base.FloatFormatHandling, QuoteChar, bool_0: true), JsonToken.Float);
		}

		public override void WriteValue(bool value)
		{
			method_17(JsonToken.Boolean);
			method_21(JsonConvert.ToString(value), JsonToken.Boolean);
		}

		public override void WriteValue(short value)
		{
			method_17(JsonToken.Integer);
			method_24(value);
		}

		[CLSCompliant(false)]
		public override void WriteValue(ushort value)
		{
			method_17(JsonToken.Integer);
			method_24(value);
		}

		public override void WriteValue(char value)
		{
			method_17(JsonToken.String);
			method_21(JsonConvert.ToString(value), JsonToken.String);
		}

		public override void WriteValue(byte value)
		{
			method_17(JsonToken.Integer);
			method_24(value);
		}

		[CLSCompliant(false)]
		public override void WriteValue(sbyte value)
		{
			method_17(JsonToken.Integer);
			method_24(value);
		}

		public override void WriteValue(decimal value)
		{
			method_17(JsonToken.Float);
			method_21(JsonConvert.ToString(value), JsonToken.Float);
		}

		public override void WriteValue(DateTime value)
		{
			method_17(JsonToken.Date);
			value = Class185.smethod_2(value, base.DateTimeZoneHandling);
			if (string.IsNullOrEmpty(base.DateFormatString))
			{
				method_23();
				int num = 0;
				char[] array = char_2;
				num = 1;
				array[0] = char_1;
				num = Class185.smethod_17(char_2, 1, value, null, value.Kind, base.DateFormatHandling);
				char_2[num++] = char_1;
				textWriter_0.Write(char_2, 0, num);
			}
			else
			{
				textWriter_0.Write(char_1);
				textWriter_0.Write(value.ToString(base.DateFormatString, base.Culture));
				textWriter_0.Write(char_1);
			}
		}

		public override void WriteValue(byte[] value)
		{
			if (value == null)
			{
				WriteNull();
				return;
			}
			method_17(JsonToken.Bytes);
			textWriter_0.Write(char_1);
			Class143_0.method_0(value, 0, value.Length);
			Class143_0.method_1();
			textWriter_0.Write(char_1);
		}

		public override void WriteValue(DateTimeOffset value)
		{
			method_17(JsonToken.Date);
			if (string.IsNullOrEmpty(base.DateFormatString))
			{
				method_23();
				int num = 0;
				char[] array = char_2;
				num = 1;
				array[0] = char_1;
				num = Class185.smethod_17(char_2, 1, (base.DateFormatHandling == DateFormatHandling.IsoDateFormat) ? value.DateTime : value.UtcDateTime, value.Offset, DateTimeKind.Local, base.DateFormatHandling);
				char_2[num++] = char_1;
				textWriter_0.Write(char_2, 0, num);
			}
			else
			{
				textWriter_0.Write(char_1);
				textWriter_0.Write(value.ToString(base.DateFormatString, base.Culture));
				textWriter_0.Write(char_1);
			}
		}

		public override void WriteValue(Guid value)
		{
			method_17(JsonToken.String);
			method_21(JsonConvert.smethod_6(value, char_1), JsonToken.String);
		}

		public override void WriteValue(TimeSpan value)
		{
			method_17(JsonToken.String);
			method_21(JsonConvert.smethod_7(value, char_1), JsonToken.String);
		}

		public override void WriteValue(Uri value)
		{
			if (value == null)
			{
				WriteNull();
				return;
			}
			method_17(JsonToken.String);
			method_21(JsonConvert.smethod_8(value, char_1), JsonToken.String);
		}

		public override void WriteComment(string text)
		{
			method_19();
			textWriter_0.Write("/*");
			textWriter_0.Write(text);
			textWriter_0.Write("*/");
		}

		public override void WriteWhitespace(string ws)
		{
			method_18(ws);
			textWriter_0.Write(ws);
		}

		private void method_23()
		{
			if (char_2 == null)
			{
				char_2 = new char[64];
			}
		}

		private void method_24(long long_0)
		{
			method_23();
			if (long_0 >= 0L && long_0 <= 9L)
			{
				textWriter_0.Write((char)(48L + long_0));
				return;
			}
			ulong ulong_ = (ulong)((long_0 < 0L) ? (-long_0) : long_0);
			if (long_0 < 0L)
			{
				textWriter_0.Write('-');
			}
			method_25(ulong_);
		}

		private void method_25(ulong ulong_0)
		{
			method_23();
			if (ulong_0 <= 9L)
			{
				textWriter_0.Write((char)(48L + ulong_0));
				return;
			}
			int num = Class193.smethod_0(ulong_0);
			int num2 = 0;
			do
			{
				char_2[num - ++num2] = (char)(48L + ulong_0 % 10uL);
				ulong_0 /= 10uL;
			}
			while (ulong_0 != 0L);
			textWriter_0.Write(char_2, 0, num2);
		}
	}
}
